#include "employee_info.h"
#include "ui_employee_info.h"
#include <QSplitter>
#include "log/log_init.h"
EmployeeInfo::EmployeeInfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::EmployeeInfo)
{
    ui->setupUi(this);
    model = new QStandardItemModel(ui->treeView);//创建模型
    ui->treeView->setModel(model);//导入模型
    ui->treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    connect(ui->treeView,&QTreeView::clicked,this,&EmployeeInfo::itemClicked);
    connect(ui->treeView,&QTreeView::expanded,this,&EmployeeInfo::expandedSlot);
    model->setHorizontalHeaderLabels(QStringList()<<QStringLiteral("SosPlus"));
    initDeptInfo();
    buildTree();
    ui->treeView->expandAll();
}

EmployeeInfo::~EmployeeInfo()
{
    delete ui;
}

void EmployeeInfo::initDeptInfo()
{
    depts = DBManager::getInstance()->selectDeptQuery(SELECT_CUSTOMER_DATA);
    deptidForDeptName.clear();
    for(auto it :depts){
        deptidForDeptName[it.deptid] = it.deptname;
    }
}

void EmployeeInfo::initUserInfo(QString deptid)
{
    auto userList = DBManager::getInstance()->select(QString(SELECT_USERS_BY_DEPTID).arg(deptid));
    updateUser(userList);
}

void EmployeeInfo::buildTree()
{
    QStandardItem *parentItem;
    for (auto it : depts) {

        QStandardItem *item = new QStandardItem(QString::number(it.deptid));
        item->setFont(QFont( "Times", 12));
        item->setIcon(QIcon(":/post_1.png"));
        item->setText(it.deptname);
        if(it.parentid==0){
            parentItem = model->invisibleRootItem();
            auto userList = DBManager::getInstance()->select(SELECT_USERS_BY_DEPTIDALL);
            updateUser(userList);
            UserInfoBaseItem * user = new UserInfoBaseItem("姓名","部门","岗位","电话");
            user->setBackgroundColor(QColor());
            user->setFixedHeight(50);
            ui->verticalLayout->addWidget(user);
            for (int it =  0 ; it<users.size(); it++) {
                auto l = users[it].dept_path.split("/");
                UserInfoBaseItem * user = new UserInfoBaseItem(users[it].user_nickname,deptidForDeptName[users[it].dept_id],l.at(l.size()-1),users[it].mobile);
                user->setFixedHeight(50);
                ui->verticalLayout->addWidget(user);
            }

            ui->verticalLayout->addStretch();
        }
        else
            parentItem = deptidForItem[it.parentid];
        if(parentItem != nullptr)
            parentItem->appendRow(item);
        deptidForItem[it.deptid] = item;
        itemFordeptid[item] = it.deptid;
    }
}

void EmployeeInfo::itemClicked(QModelIndex index)
{
    clearUserItem();
    initUserInfo(QString::number(itemFordeptid[model->itemFromIndex(index)]));
    UserInfoBaseItem * user = new UserInfoBaseItem("姓名","部门","岗位","电话");
    user->setBackgroundColor(QColor());
    user->setFixedHeight(50);
    ui->verticalLayout->addWidget(user);
    for (int it =  0 ; it<users.size(); it++) {
        UserInfoBaseItem * user = new UserInfoBaseItem(users[it].user_nickname,deptidForDeptName[users[it].dept_id],users[it].user_job,users[it].mobile);
        user->setFixedHeight(50);
        ui->verticalLayout->addWidget(user);
    }

    ui->verticalLayout->addStretch();
    QLOG_INFO()<<__FUNCTION__<<index.data();
}

void EmployeeInfo::expandedSlot(const QModelIndex &index)
{
    ui->treeView->resizeColumnToContents(index.column());

}

void EmployeeInfo::clearUserItem()
{
    QList<UserInfoBaseItem*> btns = ui->scrollAreaWidgetContents->findChildren<UserInfoBaseItem*>();//获取布局中所有按钮
    foreach (UserInfoBaseItem *btn, btns) {
        delete btn;    //析构所有按钮
    }
    for (int i = 0; i <  ui->verticalLayout->count(); ++i)
    {
        QLayoutItem *layoutItem = ui->verticalLayout->itemAt(i);

        if (layoutItem->spacerItem())
        {
            ui->verticalLayout->removeItem(layoutItem);
            --i;
        }
    }
    users.clear();
}

void EmployeeInfo::updateUser(QList<QList<QString>> userList)
{
    for(auto it :userList){
        UserInfo info;
        info.id                    =    it.at(CMF_USER_ID).toInt();
        info.user_type             =    it.at(CMF_USER_USER_TYYE).toInt();
        info.sex                   =    it.at(CMF_USER_SEX).toInt();
        info.birthday              =    it.at(CMF_USER_BIRTHDAY);
        info.last_login_time       =    it.at(CMF_USER_LAST_LOGIN_TIME);
        info.score                 =    it.at(CMF_USER_SCORE);
        info.coin                  =    it.at(CMF_USER_COIN);
        info.balance               =    it.at(CMF_USER_BALANCE);
        info.create_time           =    it.at(CMF_USER_CREATE_TIME);
        info.user_status           =    it.at(CMF_USER_USER_STATUS);
        info.user_login            =    it.at(CMF_USER_USER_LOGIN);
        info.user_pass             =    it.at(CMF_USER_USER_PASS);
        info.user_nickname         =    it.at(CMF_USER_USER_NICKNAME);
        info.user_email            =    it.at(CMF_USER_USER_EMAIL);
        info.user_url              =    it.at(CMF_USER_USER_URL);
        info.avatar                =    it.at(CMF_USER_AVATAR);
        info.signature             =    it.at(CMF_USER_SIGNATURE);
        info.last_login_ip         =    it.at(CMF_USER_LAST_LOGIN_IP);
        info.user_activation_key   =    it.at(CMF_USER_USER_ACTIVATION_KEY);
        info.mobile                =    it.at(CMF_USER_MOBILE);
        info.more                  =    it.at(CMF_USER_MORE);
        info.dept_id               =    it.at(CMF_USER_DEPT_ID).toInt();
        info.dept_path             =    it.at(CMF_USER_DEPT_PATH);
        //info.user_job              =    it.at(CMF_USER_JOB);
        users.append(info);
    }
}


